package edu.sandau.b23015103.b23015103.Controller;

import edu.sandau.b23015103.b23015103.mode.Hobby;
import edu.sandau.b23015103.b23015103.service.HobbyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.UUID;
@Controller
@RequestMapping("hobby")
public class HobbyController {
    @Autowired
    private HobbyService hobbyService;
    @RequestMapping("list")
    public String list(ModelMap map, Hobby hobby){
        if ("".equals(hobby.getName()))
        {
            hobby.setName(null);
        }
        if ("".equals(hobby.getHhobby()))
        {
            hobby.setHhobby(null);
        }
        map.put("hobbylist",hobbyService.finAll(hobby));
        return "hobby/list";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id){
        hobbyService.deleteById(id);
        return "redirect:/hobby/list";
    }

    @RequestMapping("create")
    public String create(){
        return "hobby/create";
    }

    @RequestMapping("save")
    public String save(Hobby s) {
        if(s.getId() == null){
            s.setId(UUID.randomUUID().toString());
        }
        hobbyService.save(s);
        return "redirect:/hobby/list";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable String id,ModelMap map) {
        Hobby s =hobbyService.getReferenceById(id);
        map.put("hobby",s);
        return "hobby/update";
    }
}
